//
//  XiuGaiPhoneNumberViewController.m
//  ZhiBoYuan
//
//  Created by coder_di on 2018/4/17.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "XiuGaiPhoneNumberViewController.h"
#import "NewPhoneNumViewController.h"

@interface XiuGaiPhoneNumberViewController ()<UITextFieldDelegate>
@property (nonatomic, strong)UITextField *yanzhengTextfield;
@property (nonatomic, strong)UIButton *chongFaBtn;
@property (nonatomic, strong)UIButton *xiayibuBtn;
@property (nonatomic, assign)int codeNum;
@end

@implementation XiuGaiPhoneNumberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _codeNum = 60;
    
    [self sendMs];
    
    self.view.backgroundColor = hui1Color;
    self.title = @"验证当前手机号";
    [self setUI];
    
}




-(void)sendMs{
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{@"phone":[ZYUserManager userInfo].phone,
                       @"type":@"backPwd"
                       };

    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Register&a=getSmsCode",HTTPP] parameter:parameter sucess:^(id responseObject) {
        NSLog(@"%@",responseObject);

        if ([responseObject[@"status"] intValue] == 200) {
            [NSTimer scheduledTimerWithTimeInterval:1.0
                                             target:self
                                           selector:@selector(handleMaxShowTimer:)
                                           userInfo:nil
                                            repeats:YES];
        }


    } failure:^(NSError *error) {
        NSLog(@"%@",error);

    }];

}

#pragma mark  - 验证码倒计时
- (void)handleMaxShowTimer:(NSTimer *)timer{
    
    _codeNum--;
    [_chongFaBtn setTitle:[NSString stringWithFormat:@"重新获取(%ld)",(long)_codeNum]   forState:UIControlStateNormal];
    _chongFaBtn.userInteractionEnabled = NO;
    _chongFaBtn.backgroundColor = hui2Color;
    if (_codeNum == 0) {
        NSLog(@"验证码超时");
        //        _codeNum = @"fsdsa";
        [_chongFaBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        [timer invalidate];
        timer = nil;
        _codeNum = 60;
        _chongFaBtn.userInteractionEnabled = YES;
        _chongFaBtn.backgroundColor = ZhuTiColor;
    }
    
}


-(void)setUI{
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 20 * KscreenHeight + NAVIGATION_BAR_HEIGHT, ScreenWidth, 20 * KscreenHeight)];
    lable.text = @"请输入下方手机号收到的验证码并进行验证";
    lable.font = [UIFont systemFontOfSize:16 * KscreenWidth];
    lable.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:lable];
    
    UILabel *phoneNumber = [[UILabel alloc]initWithFrame:CGRectMake(0, 60 * KscreenHeight + NAVIGATION_BAR_HEIGHT, ScreenWidth, 30 * KscreenHeight)];
    phoneNumber.text = [ZYUserManager userInfo].phone;
    phoneNumber.font = [UIFont systemFontOfSize:20 * KscreenWidth];
    phoneNumber.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:phoneNumber];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 110 * KscreenHeight + NAVIGATION_BAR_HEIGHT, ScreenWidth, 50 * KscreenHeight)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    UILabel *yanzhengLable = [[UILabel alloc]initWithFrame:CGRectMake(20 * KscreenWidth, 10 * KscreenHeight,60 *  KscreenWidth, 30  * KscreenHeight)];
    yanzhengLable.text = @"验证码:";
    yanzhengLable.font = [UIFont systemFontOfSize:18];
    yanzhengLable.textColor = hui7Color;
    [view addSubview:yanzhengLable];
    
    _yanzhengTextfield = [[UITextField alloc]initWithFrame:CGRectMake(90 * KscreenWidth,15 * KscreenHeight, 150 * KscreenWidth, 20 * KscreenHeight)];
    _yanzhengTextfield.textAlignment = NSTextAlignmentLeft;
    _yanzhengTextfield.placeholder = @"请输入4位验证码";
    _yanzhengTextfield.font = [UIFont systemFontOfSize:18];
    _yanzhengTextfield.textColor = hui7Color;
    _yanzhengTextfield.delegate = self;
    [view addSubview:_yanzhengTextfield];
    
    _chongFaBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _chongFaBtn.frame = CGRectMake(ScreenWidth - 120 * KscreenWidth, 0, 118 * KscreenWidth, 50 * KscreenHeight);
    _chongFaBtn.backgroundColor = ZhuTiColor;
    _chongFaBtn.layer.masksToBounds = YES;
    _chongFaBtn.layer.cornerRadius = 8 * KscreenWidth;
     [_chongFaBtn setTitle:@"重发验证码" forState:0];
    
    [_chongFaBtn addTarget:self action:@selector(chongfaAction) forControlEvents:(UIControlEventTouchUpInside)];
    [view addSubview:_chongFaBtn];
    
    _xiayibuBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _xiayibuBtn.frame = CGRectMake(20 * KscreenWidth, 200 * KscreenHeight + NAVIGATION_BAR_HEIGHT, ScreenWidth - 40 * KscreenWidth, 50 * KscreenHeight);
    _xiayibuBtn.layer.masksToBounds = YES;
    _xiayibuBtn.userInteractionEnabled=NO;//不能点击
    _xiayibuBtn.layer.cornerRadius = 8 * KscreenWidth;
    _xiayibuBtn.backgroundColor = hui5Color;
    [_xiayibuBtn setTitle:@"下一步" forState:0];
    [_xiayibuBtn addTarget:self action:@selector(xiayibuAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.view addSubview:_xiayibuBtn];
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (_yanzhengTextfield.text.length == 3) {
        _xiayibuBtn.backgroundColor = ZhuTiColor;
        _xiayibuBtn.userInteractionEnabled=YES;
    }else{
        _xiayibuBtn.backgroundColor = hui5Color;
        _xiayibuBtn.userInteractionEnabled=NO;
    }
    return YES;
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_yanzhengTextfield resignFirstResponder];
}

-(void)okAction{
    
    UIViewController *viewCtl = self.navigationController.viewControllers[1];
    [self.navigationController popToViewController:viewCtl animated:YES];
}

-(void)xiayibuAction{
    NewPhoneNumViewController *newPhoneVC = [[NewPhoneNumViewController alloc]init];
    newPhoneVC.honeCode = _yanzhengTextfield.text;
    [self.navigationController pushViewController:newPhoneVC animated:YES];
    
}

-(void)chongfaAction{
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
